package com.qm.ms005.service;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
//@Scope("prototype")
public class TestService {

    int i = 0;

    @Autowired
    RestHighLevelClient restHighLevelClient;

    //高亮

    public List<Map<String, Object>> searchPageHighlightBuilder(String keyword, int pageNo, int pageSize) throws IOException {

        if (pageNo <= 1) {
            pageNo = 1;
        }
        //条件搜索。

        SearchRequest searchRequest = new SearchRequest("student");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //分页
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);

        //精准匹配
        MatchQueryBuilder termQueryBuilder = QueryBuilders.matchQuery("sAddress", keyword);
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("sAddress");
        highlightBuilder.requireFieldMatch(false);//关闭多个高亮，显示一个即可。
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        sourceBuilder.highlighter(highlightBuilder);

        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);


        //结果解析
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {

            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField sAddress = highlightFields.get("sAddress");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();//原来的结果
            //解析高亮字段
            if(sAddress != null){
                Text[] fragments = sAddress.fragments();
                String n_sAddress = "";
                for(Text text : fragments){
                    n_sAddress +=  text;
                }
                sourceAsMap.put("sAddress",n_sAddress);
            }

            list.add(sourceAsMap);
        }
        return list;
    }


    public void add(){

        i++;

        System.out.println(Thread.currentThread().getName()+"__:"+Thread.currentThread().getId()+"__:"+i);


        /*new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getId());
            }
        }).start();*/
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 20, 10000, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));

        for(int i =0;i<10;i++){
            final int j = i;
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getId()+":"+j);
                }
            });
        }

    }

}
